package cn.edu.hhu.teacher.dao;

import cn.edu.hhu.Bean.AttendanceTask;
import cn.edu.hhu.util.JDBCUtils;
import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;

import java.sql.Timestamp;
import java.util.List;

public class TaskDao {
    private final JdbcTemplate template = new JdbcTemplate(JDBCUtils.getDataSource());

    public List<AttendanceTask> queryTaskByCid(long cid) {
        String sql = "select * from tasklist where cid = ?";
        try {
            return template.query(sql, new BeanPropertyRowMapper<>(AttendanceTask.class), cid);
        } catch (DataAccessException e) {
            return null;
        }
    }

    public int deleteTaskByCidAndTnum(long cid, long tnum) {
        String sql = "delete from tasklist where tnum = ? and cid = ?";
        try {
            return template.update(sql, tnum, cid);
        } catch (DataAccessException e) {
            return 0;
        }
    }

    public int addTask(long cid, String tname, String pwd, Timestamp start, Timestamp end, Timestamp release) {
        String sql = "insert into tasklist (cid, tname, pwd, start_time, end_time, release_time) values (?, ?, ?, ?, ?, ?)";
        try {
            return template.update(sql, cid, tname, pwd, start, end, release);
        } catch (DataAccessException e) {
            return 0;
        }
    }
}
